2020-04-09 18:24:37 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
2021-02-20 12:57:26 +00:00
|
|
|
#include <dlfcn.h>
|
2021-07-16 00:28:46 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <spdlog/spdlog.h>
|
2020-04-09 18:24:37 +00:00
|
|
|
#include "real_dlsym.h"
|
|
|
|
#include "mesa/util/macros.h"
|
|
|
|
#include "mesa/util/os_time.h"
|
2020-04-18 16:05:17 +00:00
|
|
|
#include "blacklist.h"
|
2020-04-09 18:24:37 +00:00
|
|
|
#include "imgui_hud.h"
|
|
|
|
|
|
|
|
using namespace MangoHud::GL;
|
|
|
|
|
|
|
|
EXPORT_C_(void *) eglGetProcAddress(const char* procName);
|
|
|
|
|
|
|
|
void* get_egl_proc_address(const char* name) {
|
|
|
|
|
|
|
|
void *func = nullptr;
|
|
|
|
static void *(*pfn_eglGetProcAddress)(const char*) = nullptr;
|
2021-01-26 08:26:44 +00:00
|
|
|
if (!pfn_eglGetProcAddress) {
|
|
|
|
void *handle = real_dlopen("libEGL.so.1", RTLD_LAZY|RTLD_LOCAL);
|
|
|
|
if (!handle) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("Failed to open " MANGOHUD_ARCH " libEGL.so.1: {}", dlerror());
|
2021-01-26 08:26:44 +00:00
|
|
|
} else {
|
|
|
|
pfn_eglGetProcAddress = reinterpret_cast<decltype(pfn_eglGetProcAddress)>(real_dlsym(handle, "eglGetProcAddress"));
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 18:24:37 +00:00
|
|
|
|
|
|
|
if (pfn_eglGetProcAddress)
|
|
|
|
func = pfn_eglGetProcAddress(name);
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
func = get_proc_address( name );
|
|
|
|
|
|
|
|
if (!func) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_DEBUG("Failed to get function '{}'", name);
|
2020-04-09 18:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
//EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
|
|
|
|
EXPORT_C_(int) eglMakeCurrent_OFF(void *dpy, void *draw, void *read,void *ctx) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_TRACE("{}: draw: {}, ctx: {}", __func__, draw, ctx);
|
2020-04-09 18:24:37 +00:00
|
|
|
int ret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_C_(unsigned int) eglSwapBuffers( void* dpy, void* surf)
|
|
|
|
{
|
|
|
|
static int (*pfn_eglSwapBuffers)(void*, void*) = nullptr;
|
|
|
|
if (!pfn_eglSwapBuffers)
|
2021-01-26 08:26:44 +00:00
|
|
|
pfn_eglSwapBuffers = reinterpret_cast<decltype(pfn_eglSwapBuffers)>(get_egl_proc_address("eglSwapBuffers"));
|
2020-04-09 18:24:37 +00:00
|
|
|
|
2020-04-18 16:05:17 +00:00
|
|
|
if (!is_blacklisted()) {
|
|
|
|
static int (*pfn_eglQuerySurface)(void* dpy, void* surface, int attribute, int *value) = nullptr;
|
|
|
|
if (!pfn_eglQuerySurface)
|
2021-01-26 08:26:44 +00:00
|
|
|
pfn_eglQuerySurface = reinterpret_cast<decltype(pfn_eglQuerySurface)>(get_egl_proc_address("eglQuerySurface"));
|
2020-04-09 18:24:37 +00:00
|
|
|
|
2020-04-18 16:05:17 +00:00
|
|
|
imgui_create(surf);
|
2020-04-09 18:24:37 +00:00
|
|
|
|
2020-04-18 16:05:17 +00:00
|
|
|
int width=0, height=0;
|
|
|
|
if (pfn_eglQuerySurface(dpy, surf, 0x3056, &height) &&
|
|
|
|
pfn_eglQuerySurface(dpy, surf, 0x3057, &width))
|
|
|
|
imgui_render(width, height);
|
2020-04-09 18:24:37 +00:00
|
|
|
|
2021-04-17 19:28:08 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
if (fps_limit_stats.targetFrameTime > 0s){
|
|
|
|
fps_limit_stats.frameStart = Clock::now();
|
|
|
|
FpsLimiter(fps_limit_stats);
|
|
|
|
fps_limit_stats.frameEnd = Clock::now();
|
|
|
|
}
|
2020-04-18 16:05:17 +00:00
|
|
|
}
|
2020-04-09 18:24:37 +00:00
|
|
|
|
|
|
|
return pfn_eglSwapBuffers(dpy, surf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct func_ptr {
|
|
|
|
const char *name;
|
|
|
|
void *ptr;
|
|
|
|
};
|
|
|
|
|
2021-02-20 20:37:31 +00:00
|
|
|
static std::array<const func_ptr, 2> name_to_funcptr_map = {{
|
2020-04-09 18:24:37 +00:00
|
|
|
#define ADD_HOOK(fn) { #fn, (void *) fn }
|
|
|
|
ADD_HOOK(eglGetProcAddress),
|
2021-02-20 20:37:31 +00:00
|
|
|
ADD_HOOK(eglSwapBuffers),
|
2020-04-09 18:24:37 +00:00
|
|
|
#undef ADD_HOOK
|
|
|
|
}};
|
|
|
|
|
2020-04-21 13:56:46 +00:00
|
|
|
EXPORT_C_(void *) mangohud_find_egl_ptr(const char *name)
|
2020-04-09 18:24:37 +00:00
|
|
|
{
|
2020-04-18 16:05:17 +00:00
|
|
|
if (is_blacklisted())
|
|
|
|
return nullptr;
|
|
|
|
|
2020-04-09 18:24:37 +00:00
|
|
|
for (auto& func : name_to_funcptr_map) {
|
|
|
|
if (strcmp(name, func.name) == 0)
|
|
|
|
return func.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_C_(void *) eglGetProcAddress(const char* procName) {
|
2021-02-20 20:37:31 +00:00
|
|
|
void* real_func = get_egl_proc_address(procName);
|
2020-04-21 13:56:46 +00:00
|
|
|
void* func = mangohud_find_egl_ptr(procName);
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_TRACE("{}: proc: {}, real: {}, fun: {}", __func__, procName, real_func, func);
|
2021-02-20 20:37:31 +00:00
|
|
|
if (func && real_func)
|
2020-04-09 18:24:37 +00:00
|
|
|
return func;
|
|
|
|
|
2021-02-20 20:37:31 +00:00
|
|
|
return real_func;
|
2020-04-09 18:24:37 +00:00
|
|
|
}
|