Path: blob/master/platform/linuxbsd/wayland/detect_prime_egl.cpp
10278 views
/**************************************************************************/1/* detect_prime_egl.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifdef GLES3_ENABLED31#ifdef EGL_ENABLED3233#include "detect_prime_egl.h"3435#include "core/string/print_string.h"36#include "core/string/ustring.h"3738#include <sys/types.h>39#include <sys/wait.h>40#include <unistd.h>41#include <cstdlib>4243// To prevent shadowing warnings.44#undef glGetString4546// Runs inside a child. Exiting will not quit the engine.47void DetectPrimeEGL::create_context(EGLenum p_platform_enum) {48#if defined(GLAD_ENABLED)49if (!gladLoaderLoadEGL(nullptr)) {50print_verbose("Unable to load EGL, GPU detection skipped.");51quick_exit(1);52}53#endif5455EGLDisplay egl_display = EGL_NO_DISPLAY;5657if (GLAD_EGL_VERSION_1_5) {58egl_display = eglGetPlatformDisplay(p_platform_enum, nullptr, nullptr);59} else if (GLAD_EGL_EXT_platform_base) {60#ifdef EGL_EXT_platform_base61egl_display = eglGetPlatformDisplayEXT(p_platform_enum, nullptr, nullptr);62#endif63} else {64egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);65}6667EGLConfig egl_config;68EGLContext egl_context = EGL_NO_CONTEXT;6970eglInitialize(egl_display, nullptr, nullptr);7172#if defined(GLAD_ENABLED)73if (!gladLoaderLoadEGL(egl_display)) {74print_verbose("Unable to load EGL, GPU detection skipped.");75quick_exit(1);76}77#endif7879eglBindAPI(EGL_OPENGL_API);8081EGLint attribs[] = {82EGL_RED_SIZE,831,84EGL_BLUE_SIZE,851,86EGL_GREEN_SIZE,871,88EGL_DEPTH_SIZE,8924,90EGL_NONE,91};9293EGLint config_count = 0;94eglChooseConfig(egl_display, attribs, &egl_config, 1, &config_count);9596EGLint context_attribs[] = {97EGL_CONTEXT_MAJOR_VERSION, 3,98EGL_CONTEXT_MINOR_VERSION, 3,99EGL_NONE100};101102egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attribs);103if (egl_context == EGL_NO_CONTEXT) {104print_verbose("Unable to create an EGL context, GPU detection skipped.");105quick_exit(1);106}107108eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);109}110111int DetectPrimeEGL::detect_prime(EGLenum p_platform_enum) {112pid_t p;113int priorities[4] = {};114String vendors[4];115String renderers[4];116117for (int i = 0; i < 4; ++i) {118vendors[i] = "Unknown";119renderers[i] = "Unknown";120}121122for (int i = 0; i < 4; ++i) {123int fdset[2];124125if (pipe(fdset) == -1) {126print_verbose("Failed to pipe(), using default GPU");127return 0;128}129130// Fork so the driver initialization can crash without taking down the engine.131p = fork();132133if (p > 0) {134// Main thread135136int stat_loc = 0;137char string[201];138string[200] = '\0';139140close(fdset[1]);141142waitpid(p, &stat_loc, 0);143144if (!stat_loc) {145// No need to do anything complicated here. Anything less than146// PIPE_BUF will be delivered in one read() call.147// Leave it 'Unknown' otherwise.148if (read(fdset[0], string, sizeof(string) - 1) > 0) {149vendors[i] = string;150renderers[i] = string + strlen(string) + 1;151}152}153154close(fdset[0]);155} else {156// In child, exit() here will not quit the engine.157158// Prevent false leak reports as we will not be properly159// cleaning up these processes, and fork() makes a copy160// of all globals.161CoreGlobals::leak_reporting_enabled = false;162163char string[201];164165close(fdset[0]);166167setenv("DRI_PRIME", itos(i).utf8().ptr(), 1);168169create_context(p_platform_enum);170171PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)eglGetProcAddress("glGetString");172const char *vendor = (const char *)glGetString(GL_VENDOR);173const char *renderer = (const char *)glGetString(GL_RENDERER);174175unsigned int vendor_len = strlen(vendor) + 1;176unsigned int renderer_len = strlen(renderer) + 1;177178if (vendor_len + renderer_len >= sizeof(string)) {179renderer_len = 200 - vendor_len;180}181182memcpy(&string, vendor, vendor_len);183memcpy(&string[vendor_len], renderer, renderer_len);184185if (write(fdset[1], string, vendor_len + renderer_len) == -1) {186print_verbose("Couldn't write vendor/renderer string.");187}188close(fdset[1]);189190// The function quick_exit() is used because exit() will call destructors on static objects copied by fork().191// These objects will be freed anyway when the process finishes execution.192quick_exit(0);193}194}195196int preferred = 0;197int priority = 0;198199if (vendors[0] == vendors[1]) {200print_verbose("Only one GPU found, using default.");201return 0;202}203204for (int i = 3; i >= 0; --i) {205const Vendor *v = vendor_map;206while (v->glxvendor) {207if (v->glxvendor == vendors[i]) {208priorities[i] = v->priority;209210if (v->priority >= priority) {211priority = v->priority;212preferred = i;213}214}215++v;216}217}218219print_verbose("Found renderers:");220for (int i = 0; i < 4; ++i) {221print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));222}223224print_verbose("Using renderer: " + renderers[preferred]);225return preferred;226}227228#endif // EGL_ENABLED229#endif // GLES3_ENABLED230231232